package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.TbRoleMapper;
import com.zhentao.mapper.TbRoleMenuMapper;
import com.zhentao.mapper.TbUserRoleMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.pojo.management.TbRole;
import com.zhentao.model.common.pojo.management.TbRoleMenu;
import com.zhentao.model.common.pojo.management.TbUserRole;
import com.zhentao.model.common.pojo.management.UserInfo;
import com.zhentao.model.common.pojo.management.dto.TbRoleMenuDto;
import com.zhentao.model.common.pojo.pay.CdzZnx;
import com.zhentao.service.ITbRoleMenuService;
import com.zhentao.userLocal.AdminUserThreadLocalUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mp
 * @since 2025-01-03
 */
@Service
public class TbRoleMenuServiceImpl extends ServiceImpl<TbRoleMenuMapper, TbRoleMenu> implements ITbRoleMenuService {
    @Resource
    private TbUserRoleMapper sysUserRoleMapper;
    @Resource
    private TbRoleMenuMapper roleMenuMapper;
    @Resource
    private TbRoleMapper tbRoleMapper;
    @Override
    public ResponseResult addRoleMenu(TbRoleMenuDto tbRoleMenu) {
        //判断权限
        TbRole role = getRole();
        if (!role.getRoleName().equals("超级管理员")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //判断参数
        if (tbRoleMenu == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取菜单id
       String menuIds = tbRoleMenu.getMenuIds();
        //转换为list集合
        List<List<Integer>> list = new ArrayList<>();
        List<List<Integer>> list1 = JSON.parseObject(menuIds, list.getClass());
        // 添加到set集合
        Set<Integer> set = new HashSet<>();
        for (List<Integer> integers : list1) {
            set.addAll(integers);
        }
        //获取id集合先删除
        List<Integer> ids = roleMenuMapper.selectList(Wrappers.<TbRoleMenu>lambdaQuery()
                        .eq(TbRoleMenu::getRoleId, tbRoleMenu.getRoleId())
                        .in(TbRoleMenu::getMenuId, set))
                .stream()
                .map(TbRoleMenu::getId)
                .collect(Collectors.toList());


        List<TbRoleMenu> roleMenuList = new ArrayList<>();

        // 遍历集合
        for (Integer integer1 : set) {
            TbRoleMenu tbRoleMenu1 = new TbRoleMenu();
            tbRoleMenu1.setRoleId(tbRoleMenu.getRoleId());
            tbRoleMenu1.setMenuId(integer1);


            roleMenuList.add(tbRoleMenu1);
        }
        if (ids.size() > 0) {
            this.removeBatchByIds(ids);
        }
        boolean b = this.saveBatch(roleMenuList);
        if (b) {
            return ResponseResult.okResult("添加成功");
        }
        return ResponseResult.errorResult(500,"添加失败");
    }

    private TbRole getRole() {
        UserInfo sysUser = AdminUserThreadLocalUtils.getUserInfo();
        TbUserRole userRole = sysUserRoleMapper.selectOne(Wrappers.<TbUserRole>lambdaQuery()
                .eq(TbUserRole::getUserId, sysUser.getId()));
        TbRole tbRole = tbRoleMapper.selectOne(Wrappers.<TbRole>lambdaQuery().eq(TbRole::getId, userRole.getRoleId()));

        return tbRole;
    }
    @Override
    public ResponseResult List(CdzZnx cdzZnx) {
        List<CdzZnx> cdzZnxList = sysUserRoleMapper.List(cdzZnx);
        return ResponseResult.okResult(cdzZnxList);
    }
}
